package com.rockcent.mall.lib.repository;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.rockcent.common.common.PageResult;
import com.rockcent.common.common.SQLPageDoing;
import com.rockcent.mall.lib.domain.enumclass.CustomerFeedbackSource;
import com.rockcent.mall.lib.dto.UserFeedbackDto;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.namedparam.NamedParameterJdbcTemplate;
import org.springframework.stereotype.Repository;

/**
 * Created by DK on 17/3/31.
 */
@Repository
public class UserFeedbackRepositoryImpl implements UserFeedbackRepositoryCustom {

    private static final Logger log = LoggerFactory.getLogger(UserFeedbackRepositoryImpl.class);

    @Autowired
    NamedParameterJdbcTemplate jdbcTemplate;

    @Autowired
    private ObjectMapper objectMapper;

    @Override
    public PageResult<UserFeedbackDto> findUserFeedbackPage(String operatorAccount, String source, PageResult page) {
        SQLPageDoing<UserFeedbackDto> doing = (sql, fields, params, typeReferenceMap) -> {
            sql.append("select %s from user_feedback uf where 1=1 ");
            if (StringUtils.isNotBlank(operatorAccount)) {
                sql.append(" and uf.operator_account = :operatorAccount");
                params.put("operatorAccount", operatorAccount);
            }
            if (CustomerFeedbackSource.ALL != CustomerFeedbackSource.valueOf(source)) {
                sql.append(" and uf.source = :source");
                params.put("source", source);
            }
            sql.append(" order by uf.date_created desc");
            fields.append("uf.*");
        };
        return doing.go(page, UserFeedbackDto.class, objectMapper, jdbcTemplate, log);
    }
}
